package com.zbkj.admin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zbkj.admin.dao.UserBankAccountDao;
import com.zbkj.admin.exception.CrmebException;
import com.zbkj.admin.model.user.UserBankAccount;
import com.zbkj.admin.request.UserBankAccountRequest;
import com.zbkj.admin.result.CommonResultCode;
import com.zbkj.admin.service.CityRegionService;
import com.zbkj.admin.service.UserBankAccountService;
import com.zbkj.admin.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 用户银行卡账号 实现接口
 *
 * @author llc
 * @since 2025-04-23
 */
@Service
public class UserBankAccountServiceImpl extends ServiceImpl<UserBankAccountDao, UserBankAccount> implements UserBankAccountService {

    @Resource
    private UserBankAccountDao dao;

    @Autowired
    private UserService userService;
    @Autowired
    private CityRegionService cityRegionService;

    /**
     * 创建银行卡账号
     *
     * @param request UserBankAccountRequest 参数
     * @return 银行卡账号ID
     */
    @Override
    public Integer create(UserBankAccountRequest request) {
        // 定义保存对象 并将参数填入
        UserBankAccount userBankAccount = new UserBankAccount();
        BeanUtil.copyProperties(request, userBankAccount);
        userBankAccount.setUid(userService.getUserIdException());

        //把当前用户其他默认银行卡账号取消
        if (userBankAccount.getIsDefault()) {
            cancelDefault(userBankAccount.getUid());
        }
        userBankAccount.setCreateTime(new Date());
        userBankAccount.setUpdateTime(new Date());
        userBankAccount.setIsDel(Boolean.FALSE);
        boolean save = save(userBankAccount);
        if (!save) {
            throw new CrmebException("银行卡账号创建失败");
        }
        return userBankAccount.getId();
    }

    /**
     * 用户银行卡账号编辑
     *
     * @param request 编辑请求参数
     * @return Boolean
     */
    @Override
    public Boolean edit(UserBankAccountRequest request) {
        if (ObjectUtil.isNull(request.getId())) {
            throw new CrmebException(CommonResultCode.VALIDATE_FAILED, "id不可为空");
        }
        // 校验此银行卡账号 是否属于此用户
        Integer userId = userService.getUserIdException();
        UserBankAccount userBankAccount = getById(request.getId());
        if (ObjectUtil.isNull(userBankAccount) || !userId.equals(userBankAccount.getUid())) {
            throw new CrmebException("银行卡账号不存在");
        }
        // 定义保存对象 赋予对象新参数
        UserBankAccount address = new UserBankAccount();
        BeanUtil.copyProperties(request, address);

        //把当前用户其他默认银行卡账号取消
        if (address.getIsDefault()) {
            cancelDefault(userId);
        }
        return updateById(address);
    }

    /**
     * 删除
     *
     * @param id Integer id
     * @return UserBankAccount
     */
    @Override
    public Boolean delete(Integer id) {
        LambdaUpdateWrapper<UserBankAccount> wrapper = Wrappers.lambdaUpdate();
        wrapper.set(UserBankAccount::getIsDel, true);
        wrapper.eq(UserBankAccount::getId, id);
        wrapper.eq(UserBankAccount::getUid, userService.getUserIdException());
        return dao.update(wrapper) > 0;
    }

    /**
     * 获取默认银行卡账号
     *
     * @return UserBankAccount
     */
    @Override
    public UserBankAccount getDefault() {
        LambdaQueryWrapper<UserBankAccount> lqw = new LambdaQueryWrapper<>();
        lqw.eq(UserBankAccount::getIsDefault, true);
        lqw.eq(UserBankAccount::getUid, userService.getUserIdException());
        lqw.eq(UserBankAccount::getIsDel, false);
        lqw.last(" limit 1");
        return dao.selectOne(lqw);
    }

    private UserBankAccount getById(Integer addressId) {
        LambdaQueryWrapper<UserBankAccount> lqw = new LambdaQueryWrapper<>();
        lqw.eq(UserBankAccount::getId, addressId);
        lqw.eq(UserBankAccount::getIsDel, false);
        lqw.last(" limit 1");
        return dao.selectOne(lqw);
    }

    /**
     * 获取银行卡账号详情
     *
     * @param id 银行卡账号id
     * @return UserBankAccount
     */
    @Override
    public UserBankAccount getDetail(Integer id) {
        Integer UserId = userService.getUserIdException();
        LambdaQueryWrapper<UserBankAccount> lqw = Wrappers.lambdaQuery();
        lqw.select(UserBankAccount::getId, UserBankAccount::getType, UserBankAccount::getBankName, UserBankAccount::getBankCode, UserBankAccount::getIsDefault);
        lqw.eq(UserBankAccount::getId, id);
        lqw.eq(UserBankAccount::getUid, UserId);
        lqw.eq(UserBankAccount::getIsDel, false);
        lqw.last(" limit 1");
        return dao.selectOne(lqw);
    }

    /**
     * 获取所有的用户银行卡账号
     *
     * @return List
     */
    @Override
    public List<UserBankAccount> getAllList() {
        Integer UserId = userService.getUserIdException();
        LambdaQueryWrapper<UserBankAccount> lqw = Wrappers.lambdaQuery();
        // 拼接查询展示数据
        lqw.select(UserBankAccount::getId, UserBankAccount::getType, UserBankAccount::getBankName, UserBankAccount::getBankCode, UserBankAccount::getIsDefault);
        // 拼接查询条件
        lqw.eq(UserBankAccount::getUid, UserId);
        lqw.eq(UserBankAccount::getIsDel, Boolean.FALSE);
        lqw.orderByDesc(UserBankAccount::getIsDefault);
        lqw.orderByDesc(UserBankAccount::getId);
        return dao.selectList(lqw);
    }

    /**
     * 设置默认银行卡账号
     *
     * @param id 银行卡账号id
     * @return Boolean
     */
    @Override
    public Boolean setDefault(Integer id) {
        UserBankAccount userBankAccount = getDefault();
        if (ObjectUtil.isNotNull(userBankAccount)) {
            cancelDefault(userBankAccount.getUid());
        }
        LambdaUpdateWrapper<UserBankAccount> wrapper = Wrappers.lambdaUpdate();
        wrapper.set(UserBankAccount::getIsDefault, true);
        wrapper.eq(UserBankAccount::getId, id);
        wrapper.eq(UserBankAccount::getUid, userService.getUserIdException());
        return update(wrapper);
    }

    /**
     * 取消默认银行卡账号
     *
     * @param userId Integer 城市id
     */
    private void cancelDefault(Integer userId) {
        LambdaUpdateWrapper<UserBankAccount> wrapper = Wrappers.lambdaUpdate();
        wrapper.set(UserBankAccount::getIsDefault, Boolean.FALSE);
        wrapper.eq(UserBankAccount::getUid, userId);
        update(wrapper);
    }

    /**
     * 根据类型获取默认银行卡账号
     */
    @Override
    public UserBankAccount getDefaultByType(String type){
        Integer UserId = userService.getUserIdException();
        LambdaQueryWrapper<UserBankAccount> lqw = Wrappers.lambdaQuery();
        // 拼接查询展示数据
        lqw.select(UserBankAccount::getId, UserBankAccount::getType, UserBankAccount::getBankName, UserBankAccount::getBankCode, UserBankAccount::getIsDefault);
        // 拼接查询条件
        lqw.eq(UserBankAccount::getUid, UserId);
        lqw.eq(UserBankAccount::getIsDel, Boolean.FALSE);
        lqw.eq(UserBankAccount::getType, type);
        lqw.orderByDesc(UserBankAccount::getIsDefault);
        lqw.orderByDesc(UserBankAccount::getId);
        List<UserBankAccount> accounts = dao.selectList(lqw);
        return accounts != null && accounts.size() > 0 ? accounts.get(0) : null;
    }
}

